package com.huike.web.controller.review;



import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;

import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult add1(@PathVariable String name,@PathVariable int age,@PathVariable String sex){
        Review review = new Review();
        review.setAge(age);
        review.setName(name);
        review.setSex(sex);
        int i = reviewService.insert(review);
        if(i>0){
            return AjaxResult.success(i);
        }else {
            return AjaxResult.error("添加失败");
        }
    }

    @PostMapping("/saveData")
    public AjaxResult add(@RequestBody Review review){
        int i = reviewService.insert(review);
        if(i>0){
            return AjaxResult.success(i);
        }else {
            return AjaxResult.error("添加失败");
        }
    }


    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable long id){
        int i = reviewService.delete(id);
        if(i>0){
            return AjaxResult.success(i);
        }else {
            return AjaxResult.error("删除失败");
        }
    }
    /**=========================================================修改数据=============================================*/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        int i = reviewService.update(review);
        if(i>0){
            return AjaxResult.success(i);
        }else {
            return AjaxResult.error("修改失败");
        }
    }
    /**=========================================================查询数据=============================================*/
    // 根据id查询
    @GetMapping("/{id}")
    public AjaxResult selectById(long id){
        Review review = reviewService.selectById(id);
        return AjaxResult.success(review);
    }

    /**
     * 分页
     * @param
     * @param req
     * @return
     */
    @GetMapping("/getDataByPage")
    public TableDataInfo pool(Long page,Long pageSize, HttpServletRequest req){
        startPage();
        List<Review> reviews = reviewService.selectByPage(page, pageSize);
        return getDataTable(reviews);
    }

}